package my.algorithms.mcorrea.topcoder;

import java.util.TreeMap;
import java.util.Map;

/**
 * http://community.topcoder.com/stat?c=problem_statement&pm=4674
 * @author mcorrea
 */
public class Decipher {

    public static void main(String[] args) {
		decipher(new String[]{ "ABBBCC" }, "XYZ");
		decipher(new String[]{ "RZLW" }, "CEFD");
		
	//	System.out.println(decipher(new String[]{ "XX YYY Z YYY XX", "WWWWWZWWWWW" }, "ETMQ"));
    }
	
	
	//NOTE: Not finish yet... 
	
	public static String[] decipher(String[] encoded, String frequencyOrder){
		String denconded[] = new String[encoded.length];
		String reversefrequencyOrder = new StringBuffer(frequencyOrder).reverse().toString();
		
		for(int k=0; k<encoded.length;k++){
			String enc = encoded[k];
			Map<String, Integer> frequency = new TreeMap<String, Integer>();
			for(char c: enc.toCharArray()){
				if(c != ' '){
					if(frequency.get(""+c)!=null){
						frequency.put(""+c, frequency.get(""+c)+1);
					}else{
						frequency.put(""+c,1);
					}
				}
			}
			
			
			
			String dec = enc;
			int count = 0;
			for(String key: frequency.keySet()){
				dec = dec.replace(key.charAt(0), 
						reversefrequencyOrder.charAt( frequency.get(key)-1  )   );
				count++;
			}
			denconded[k] = dec;
			
			System.out.println(dec);
		}
		
		return denconded;
	
	}

}
